quietly {
forv d=1/3 {
	forv bs=0/$bootstraps {
		forv o=1/10 {
			
clear      
use ${data}Simulation/sim_data_d`d'_bs`bs'.dta
keep if oo==`o'

merge m:1 schoolpos_id using ${data}Simulation/optimal_taus`d'_o`o'_bs`bs'.dta, nogen



if missing(tau1_star) continue


	gen unhired = 1
	gen unfilled = 1


	
	gen _hire1 = .
		replace _hire1 = u_sch>=tau1_star & u_sch<. & round==1 & pos==1 
	bys teacher_id: egen hire_t1 = max(_hire1)
	bys schoolpos_id: egen hire_s1 = max(_hire1)
	bys schoolcert_id: egen n_hires = sum(_hire1)
	
	replace unhired = 0 if hire_t1==1
	replace unfilled = 0 if hire_s1==1
	
	gen _hire2 = .
		replace _hire2 = u_sch>=tau2_star  & u_sch<. & round==2  & (pos==1 | pos==2 & n_hires==1) & unhired==1 & unfilled==1  // Hire in current interview
	bys teacher_id: egen hire_t2 = max(_hire2)
	bys schoolpos_id: egen hire_s2 = max(_hire2)
	bys schoolcert_id: egen _n_hires = sum(_hire2)
	replace n_hires = n_hires+_n_hires
	drop _n_hires
	replace unhired = 0 if hire_t2==1
	replace unfilled = 0 if hire_s2==1	
	
		gen _hire2r = .
		
		* Allow for recall of first round interview
		replace _hire2r = u_sch>=tau2_star  & u_sch<. & round==1  & (pos==1 | pos==2 & n_hires==1) & unhired==1 & unfilled==1 
		bys teacher_id: egen hire_t2r = max(_hire2r)
		bys schoolpos_id: egen hire_s2r = max(_hire2r)
		bys schoolcert_id: egen _n_hires = sum(_hire2r)
		replace n_hires = n_hires+_n_hires
		replace unhired = 0 if hire_t2r==1
		replace unfilled = 0 if hire_s2r==1	
		drop _n_hires

	
	gen _hire3 = .
		replace _hire3 = u_sch>=tau3_star & u_sch<. & round==3 & (pos==1 | pos==2 & n_hires==1)  & unhired==1 & unfilled==1 
	bys teacher_id: egen hire_t3 = max(_hire3)
	bys schoolpos_id: egen hire_s3 = max(_hire3)
	bys schoolcert_id: egen _n_hires = sum(_hire3)
	replace n_hires = n_hires+_n_hires
	drop _n_hires
	replace unhired = 0 if hire_t3==1
	replace unfilled = 0 if hire_s3==1	
	
		gen _hire3r = .
		
		* Allow for recall of earlier interviews
		foreach r in 2 1 {
			replace _hire3r = u_sch>=tau3_star  & u_sch<. & round==`r'  & (pos==1 | pos==2 & n_hires==1) & unhired==1 & unfilled==1 
			bys teacher_id: egen hire_t3r = max(_hire3r)
			bys schoolpos_id: egen hire_s3r = max(_hire3r)
			bys schoolcert_id: egen _n_hires = sum(_hire3r)
			replace n_hires = n_hires+_n_hires
			replace unhired = 0 if hire_t3r==1
			replace unfilled = 0 if hire_s3r==1	
			drop hire_t3r hire_s3r _n_hires
		}
		bys teacher_id: egen hire_t3r = max(_hire3r)
		bys schoolpos_id: egen hire_s3r = max(_hire3r)
	
	gen _hire4 = .
		replace _hire4 = u_sch>=tau4_star & u_sch<. & round==4   & (pos==1 | pos==2 & n_hires==1)  & unhired==1 & unfilled==1 	
	bys teacher_id: egen hire_t4 = max(_hire4)
	bys schoolpos_id: egen hire_s4 = max(_hire4)
	bys schoolcert_id: egen _n_hires = sum(_hire4)
	replace n_hires = n_hires+_n_hires
	drop _n_hires
	replace unhired = 0 if hire_t4==1
	replace unfilled = 0 if hire_s4==1	
	
		gen _hire4r = .
	
		* Allow for recall of earlier interviews
		foreach r in 3 2 1 {
			replace _hire4r = u_sch>=tau4_star  & u_sch<. & round==`r'  & (pos==1 | pos==2 & n_hires==1) & unhired==1 & unfilled==1 
			bys teacher_id: egen hire_t4r = max(_hire4r)
			bys schoolpos_id: egen hire_s4r = max(_hire4r)
			bys schoolcert_id: egen _n_hires = sum(_hire4r)
			replace n_hires = n_hires+_n_hires
			replace unhired = 0 if hire_t4r==1
			replace unfilled = 0 if hire_s4r==1	
			drop hire_t4r hire_s4r _n_hires
		}
		bys teacher_id: egen hire_t4r = max(_hire4r)
		bys schoolpos_id: egen hire_s4r = max(_hire4r)
	
	
	gen _hire5 = .
		replace _hire5 = u_sch>=tau5_star  & u_sch<. & round==5 & (pos==1 | pos==2 & n_hires==1)  & unhired==1 & unfilled==1 
	bys teacher_id: egen hire_t5 = max(_hire5)
	bys schoolpos_id: egen hire_s5 = max(_hire5)
	bys schoolcert_id: egen _n_hires = sum(_hire5)
	replace n_hires = n_hires+_n_hires
	drop _n_hires
	replace unhired = 0 if hire_t5==1
	replace unfilled = 0 if hire_s5==1	
	
	

		gen _hire5r = .
		foreach r in 4 3 2 1 {
			replace _hire5r = u_sch>=tau5_star  & u_sch<. & round==`r'  & (pos==1 | pos==2 & n_hires==1) & unhired==1 & unfilled==1  
			bys teacher_id: egen hire_t5r = max(_hire5r)
			bys schoolpos_id: egen hire_s5r = max(_hire5r)
			bys schoolcert_id: egen _n_hires = sum(_hire5r)
			replace n_hires = n_hires+_n_hires
			replace unhired = 0 if hire_t5r==1
			replace unfilled = 0 if hire_s5r==1	
			drop hire_t5r hire_s5r _n_hires
		}
		bys teacher_id: egen hire_t5r = max(_hire5r)
		bys schoolpos_id: egen hire_s5r = max(_hire5r)
	
	
	gen _hire6 = .
		replace _hire6 = u_sch>=tau6_star & u_sch<. & round==6 & (pos==1 | pos==2 & n_hires==1)  & unhired==1 & unfilled==1 
	bys teacher_id: egen hire_t6 = max(_hire6)
	bys schoolpos_id: egen hire_s6 = max(_hire6)
	bys schoolcert_id: egen _n_hires = sum(_hire6)
	replace n_hires = n_hires+_n_hires
	drop _n_hires
	replace unhired = 0 if hire_t6==1
	replace unfilled = 0 if hire_s6==1	
	
	
		gen _hire6r = .
		foreach r in 5 4 3 2 1 {
			replace _hire6r = u_sch>=tau6_star  & u_sch<. & round==`r'  & (pos==1 | pos==2 & n_hires==1) & unhired==1 & unfilled==1 
			bys teacher_id: egen hire_t6r = max(_hire6r)
			bys schoolpos_id: egen hire_s6r = max(_hire6r)
			bys schoolcert_id: egen _n_hires = sum(_hire6r)
			replace n_hires = n_hires+_n_hires
			replace unhired = 0 if hire_t6r==1
			replace unfilled = 0 if hire_s6r==1	
			drop hire_t6r hire_s6r _n_hires
		}
		bys teacher_id: egen hire_t6r = max(_hire6r)
		bys schoolpos_id: egen hire_s6r = max(_hire6r)
		

gen _hire7 = .
	replace _hire7 = u_sch>=tau7_star & u_sch<.  & round==7  & (pos==1 | pos==2 & n_hires==1)  & unhired==1 & unfilled==1 
	bys teacher_id: egen hire_t7 = max(_hire7)
	bys schoolpos_id: egen hire_s7 = max(_hire7)
	replace unhired = 0 if hire_t7==1
	replace unfilled = 0 if hire_s7==1	
	

		gen _hire7r = .
		foreach r in 6 5 4 3 2 1 {
			replace _hire7r = u_sch>=tau7_star  & u_sch<. & round==`r'  & (pos==1 | pos==2 & n_hires==1) & unhired==1 & unfilled==1 
			bys teacher_id: egen hire_t7r = max(_hire7r)
			bys schoolpos_id: egen hire_s7r = max(_hire7r)
			bys schoolcert_id: egen _n_hires = sum(_hire7r)
			replace n_hires = n_hires+_n_hires
			replace unhired = 0 if hire_t7r==1
			replace unfilled = 0 if hire_s7r==1	
			drop hire_t7r hire_s7r _n_hires
		}
		bys teacher_id: egen hire_t7r = max(_hire7r)
		bys schoolpos_id: egen hire_s7r = max(_hire7r)

	


	gen fo_match = .
		replace fo_match  =  (_hire1==1 | _hire2==1  | _hire3==1 | _hire4==1 | _hire5==1 | _hire6==1 | _hire7==1 |  _hire2r==1  | _hire3r==1 | _hire4r==1 | _hire5r==1 | _hire6r==1 | _hire7r==1) & u_sch>sch_outside_option



drop _hire* hire*   unhired unfilled n_hires


keep teacher_id schoolpos_id fo_match

save ${data}Simulation/sim_fo_d`d'_o`o'_bs`bs'.dta, replace
     
}
}
}
}
	
exit
